#!/bin/sh
ECLIPSE_DIR=/apps/composer/composer-headless-6.7
PROJ_ROOT=$WORKSPACE
TARGET_ROOT=/tmp/projectx
TARGET_DIR=$TARGET_ROOT/composer-projects
INSTALL_DIR=$TARGET_DIR/dars
BUILD_FILE=composer.xml
USERNAME=dmadmin
PASSWORD=password
DOCBASE=test
LOGIN="-Ddocbase=$DOCBASE -Dusername=$USERNAME -Dpassword=$PASSWORD"

CP=$ECLIPSE_DIR/startup.jar:$WORKSPACE/deploy
CLASS=org.eclipse/core/launcher.Main
APP=org.eclipse.ant.core.antRunner

echo "ECLIPSE_DIR=$ECLIPSE_DIR"
echo "PROJ_ROOT=$PROJ_ROOT"
echo "TARGET_ROOT=$TARGET_ROOT"
echo "TARGET_DIR=$TARGET_DIR"
echo "INSTALL_DIR=$INSTALL_DIR"
echo "BUILD_FILE=$BUILD_FILE"
echo "CP=$CP"
echo "CLASS=$CLASS"
echo "APP=$APP"
echo "------------------"

create_dar() {
NAME=$1
PROJ_DIR=$TARGET_DIR/$NAME
DAR_DIR=$PROJ_DIR/bin-dar
DAR_FILE=$NAME.dar
DAR_PATH=$DAR_DIR/$DAR_FILE
OPTS="-Ddmproject=$NAME -Ddar=$DAR_PATH -Dproject=$PROJ_DIR"
TASKS="packageproject deploydar2maven"
CMD="java -cp $CP $CLASS $OPTS -data $TARGET_DIR -application $APP -buildfile $BUILD_FILE $TASKS"
echo "NAME=$NAME"
echo "PROJ_DIR=$PROJ_DIR"
echo "DAR_DIR=$DAR_DIR"
echo "DAR_FILE=$DAR_FILE"
echo "DAR_PATH=$DAR_PATH"
echo "TASKS=$TASKS"
echo "CMD=$CMD"
echo "------------------"

mkdir -p $DAR_DIR

# build the dar
java -cp $CP $CLASS -Ddmproject=$NAME -Ddar=$DAR_PATH -Dproject=$PROJ_DIR -data $TARGET_DIR -application $APP -buildfile $BUILD_FILE $TASKS

# copy dar to workspace so that it can be archived by jenkins
cp $DAR_PATH $WORKSPACE

# copy dar to separate dir so that it can be deployed by headless composer
cp $DAR_PATH $INSTALL_DIR
}

deploy_dar() {
NAME=$1
PROJ_DIR=$TARGET_DIR/$NAME
DAR_FILE=$NAME.dar
DAR_PATH=$INSTALL_DIR/$DAR_FILE
OPTS="-Ddmproject=$NAME -Ddar=$DAR_PATH -Dproject=$PROJ_DIR $LOGIN"
TASKS="installdar2documentum"
CMD="java -cp $CP $CLASS $OPTS -data $TARGET_DIR -application $APP -buildfile $BUILD_FILE $TASKS"
echo "NAME=$NAME"
echo "PROJ_DIR=$PROJ_DIR"
echo "INSTALL_DIR=$INSTALL_DIR"
echo "DAR_FILE=$DAR_FILE"
echo "DAR_PATH=$DAR_PATH"
echo "TASKS=$TASKS"
echo "CMD=$CMD"
echo "------------------"

# install dar to documentum
java -cp $CP $CLASS $OPTS -data $TARGET_DIR -application $APP -buildfile $BUILD_FILE $TASKS
}

rm -rf $TARGET_ROOT
cp -r $PROJ_ROOT $TARGET_ROOT
mkdir -p $INSTALL_DIR

create_dar projectx-base
# uncomment next line to deploy to documentum
#deploy_dar projectx-base

create_dar projectx-sbo
# uncomment next line to deploy to documentum
#deploy_dar projectx-sbo